package com.example.mouse;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class TouchPadView extends View {
    private float lastX, lastY;
    private SocketClient socketClient;

    public TouchPadView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setSocketClient(SocketClient client) {
        this.socketClient = client;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                break;

            case MotionEvent.ACTION_MOVE:
                float dx = x - lastX;
                float dy = y - lastY;

                if (socketClient != null) {
                    socketClient.sendMessage("MOVE:" + dx + "," + dy);
                }

                lastX = x;
                lastY = y;
                break;

            case MotionEvent.ACTION_UP:
                if (socketClient != null) {
                    socketClient.sendMessage("CLICK:LEFT");
                }
                break;
        }
        return true;
    }
}